package string;

public class StringDemo {
    public static void main(String[] args) {
        String s1 = "123abc";//对象会缓存到变量池中
        String s2 ="123abc";//重用对象
        System.out.println("s1:"+s1);
        System.out.println("s2:"+s2);
        System.out.println("s1==s2:"+(s1==s2));
        String s3 = "123abc";//重用对象
        System.out.println("s3:"+s3);
        System.out.println("s1==s3:"+(s1==s3));
        //new一定产生新对象,java不推荐我们使用new创建字符串
        String s4 = new String("123abc");
        System.out.println("s4:"+s4);
        System.out.println("s1==s4:"+(s1==s4));//地址不同
        /**
         * 字符串是不变对象,即:创建后内容不可变
         *拼接修改字符串内容实际上会产生新对象,因此s1的地址发生了改变
         */

        s1=s1+"!";
        System.out.println("s1:"+s1);//s1:123abc!       引用新对象
        System.out.println("s2:"+s2);//s2:123abc        还是引用常量池对象
        System.out.println("s3:"+s3);//s3:123abc        还是引用常量池对象

        String s5 = "123"+"abc";
        System.out.println("s5:"+s5);
        System.out.println("s2==s5:"+(s2==s5));//true       重用常量池对象

    }
}
